package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        // if exists, update quantity;
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if(list != null && !list.isEmpty()) {
            // Update the quantity of the existing item
            ShoppingCart existingItem = list.get(0);
            existingItem.setNumber(existingItem.getNumber() + 1);
            // Update the item in the database
             shoppingCartMapper.updateNumberById(existingItem); // Uncomment this line when update method is implemented
            return ;
        }

        // if not, add new item
        shoppingCart.setNumber(1); // Default quantity is 1
        shoppingCart.setCreateTime(LocalDateTime.now());
        // if is dish:
        if(shoppingCartDTO.getDishId() != null) {
            DishVO dish = dishMapper.getById(shoppingCartDTO.getDishId());
            shoppingCart.setName(dish.getName());
            shoppingCart.setAmount(dish.getPrice());
            shoppingCart.setImage(dish.getImage());
        }
        // if is setmeal:
        else if(shoppingCartDTO.getSetmealId() != null) {
            SetmealVO setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setAmount(setmeal.getPrice());
            shoppingCart.setImage(setmeal.getImage());
        }
        shoppingCartMapper.insert(shoppingCart);
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();

        // if is dish:
        if(shoppingCartDTO.getDishId() != null) {
            shoppingCart = ShoppingCart.builder()
                    .userId(userId)
                    .dishId(shoppingCartDTO.getDishId())
                    .dishFlavor(shoppingCartDTO.getDishFlavor())
                    .build();
        }
        // if is setmeal:
        else if(shoppingCartDTO.getSetmealId() != null) {
            shoppingCart = ShoppingCart.builder()
                    .userId(userId)
                    .setmealId(shoppingCartDTO.getSetmealId())
                    .build();
        }

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if(list != null && !list.isEmpty()) {
            ShoppingCart existingItem = list.get(0);
            if(existingItem.getNumber() > 1) {
                existingItem.setNumber(existingItem.getNumber() - 1);
                shoppingCartMapper.updateNumberById(existingItem);
            } else {
                shoppingCartMapper.deleteById(existingItem.getId());
            }
        }
    }

    /**
     * List all items in the shopping cart for the current user.
     *
     * @return List of ShoppingCart items
     */
    @Override
    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.listByUserId(userId);
        return shoppingCartList;
    }

    /**
     * Clears the shopping cart for the current user.
     */
    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(userId);
        shoppingCartMapper.deleteByUserId(userId);
    }
}
